<script setup>
import { ref } from 'vue'
import api from '@/api'
import CourseList from '@/views/CourseList.vue'
import { useRoute, useRouter } from 'vue-router'

const $router = useRouter()
const $route = useRoute()
const loading = ref(false)
const courses = ref([])

const searchCourses = async () => {
  loading.value = true
  try {
    courses.value = await api.get(`/courses`, {
      params: {
        keyword: $route.params.keyword,
      },
    })
  } catch (error) {
    console.error(error)
  }
  loading.value = false
}

const onClickLeft = () => {
  $router.replace('/')
}

searchCourses()
</script>

<template>
  <div>
    <van-nav-bar :title="`搜索：${$route.params.keyword}`" left-text="返回" left-arrow @click-left="onClickLeft"> </van-nav-bar>

    <van-loading v-if="loading" class="page-loading" type="spinner" />

    <van-empty description="未搜索到相关课程" v-else-if="!courses.length" class="page-empty" />

    <CourseList :courses="courses"></CourseList>
  </div>
</template>
